package com.github.phoenics.teacher.itheima.netty.jdkSerializable;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

/**
 * @author Phoenics Chow
 * on 2020年04月18日
 */
public class JsonMain {
    public static void main(String[] args) throws Exception {

        //序列化
        Person person = new Person();
        person.setName("混世魔王");
        person.setSinglehood(true);
        //SerializationUtil.writeObject(person);
        String json=JsonSerializationUtil.serialize(person);
        System.out.println("json==="+json);

        //为了让大家看清结果,故意加入的 段--------------开始

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(json.getBytes("utf-8"));

        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        String result = new BufferedReader(new InputStreamReader(swapStream,"utf-8"))
                .lines().collect(Collectors.joining("\n"));
        //为了让大家看清结果,故意加入的 段--------------结束


        Person p =  JsonSerializationUtil.deserialize(result,Person.class);
        System.out.println("name = " + p.getName());
        System.out.println("singlehood = " + p.isSinglehood());
    }

}
